package com.csw.shuanfa.CodeImprove.redlockThread.locktest.service;

import com.csw.shuanfa.CodeImprove.redlockThread.locktest.service.impl.DateBaseComponentImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedLockService {
    //redLock只创建一次
    @Autowired
    private DateBaseComponentImpl dateBaseComponentImpl;

    public void oversold(int reqNums) {

        try {
            for (int i = 0; i < reqNums; i++) {
                TimeUnit.MILLISECONDS.sleep(2);//防止同时去请求加锁，导致后面加锁失败
                // 一个线程模拟一个请求
                new Thread(() -> {
                    dateBaseComponentImpl.redLockDecrment();
                }).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}
